﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using KingOfCSharp.iHunter;

namespace KingOfCSharp.iHunter.UI
{
    public partial class ChannelProviderList : UserControl
    {
        private LocalClient client = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="ChannelProviderList"/> class.
        /// </summary>
        public ChannelProviderList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the client.
        /// </summary>
        /// <value>The client.</value>
        public LocalClient Client
        {
            get
            {
                return client;
            }
            set
            {
                client = value;
                RefreshList();
            }
        }
        protected void RefreshList()
        {
            if (client == null)
            {
                this.listView.Items.Clear();
                return;
            }

            this.listView.BeginUpdate();
            this.listView.Items.Clear();

            foreach (IChannelProvider provider in client.ChannelProvidersDictionary.Values)
            {
                
                if (provider.Guid == Guid.Empty)
                    continue;

                ListViewItem item = this.listView.Items.Add(provider.Name);
                item.SubItems.Add(provider.Description);
                item.Tag = provider;
            }
            this.listView.EndUpdate();
        }

        /// <summary>
        /// Gets the selected Channel provider.
        /// </summary>
        public IChannelProvider SelectedProvider
        {
            get
            {
                Guid guid = SelectedProviderGuid;
                if (guid == Guid.Empty)
                {
                    return null;
                }
                else
                {
                    return client.ChannelProvidersDictionary[guid];
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected provider GUID.
        /// </summary>
        /// <value>The selected provider GUID.</value>
        public Guid SelectedProviderGuid
        {
            get
            {
                var items = this.listView.SelectedItems;
                if (items.Count == 0)
                    return Guid.Empty;
                IChannelProvider provider = items[0].Tag as IChannelProvider;
                if (provider == null)
                    return Guid.Empty;
                return provider.Guid;

            }
            set
            {
                foreach (ListViewItem item in listView.Items)
                {
                    IChannelProvider provider = item.Tag as IChannelProvider;
                    if (provider != null && provider.Guid == value)
                        item.Selected = true;
                }
            }
        }



    }
}     

